package typedefault

import (
	"codejy/globalfunc"
	"codejy/store"
	"codejy/structdef"
	"fmt"
	"regexp"
	"strconv"
	"strings"
)

// key 实例地址,value 值

type Regexp struct {
	Addr        string
	ThreadStore *structdef.ThreadStore
}

func (f *Regexp) CreateTypeDefault(addr string, threadStore *structdef.ThreadStore, parameter ...string) {
	fUtil := &Regexp{
		Addr:        addr,
		ThreadStore: threadStore,
	}
	store.AddTypeDefaultInstanceStore(threadStore, f.GetTypeName(), addr, fUtil)
}
func (f *Regexp) GetData() interface{} {
	return nil
}

func (a *Regexp) ToStr() string {
	return ""
}
func (a *Regexp) ParameterProcessing(parameter []string) []string {
	//排除前后""
	for i := range parameter {
		s := parameter[i]
		if strings.HasPrefix(s, "\"") && strings.HasSuffix(s, "\"") {
			s = s[1 : len(s)-1]
			parameter[i] = s
		}
	}
	return parameter
}

// 验证正则表达式是否正确
func (a *Regexp) Verify(reg string) string {
	_, err := regexp.Compile(reg)
	if err != nil {
		sprintf := fmt.Sprintf("正则表达式错误:%s", err.Error())
		fmt.Println(sprintf)
		return "false"
	}
	return "true"
}

// 正则匹配,返回true,false
func (a *Regexp) Match(reg string, str string) string {
	matched, err := regexp.MatchString(reg, str)
	if err != nil {
		return "false"
	}
	return strconv.FormatBool(matched)
}

// 正则替换,全部满足条件的都替换
func (a *Regexp) Replace(reg string, str string, replace string) string {
	return regexp.MustCompile(reg).ReplaceAllString(str, replace)
}

// 正则提取,返回第一个匹配到的
func (a *Regexp) Find(reg string, str string) string {
	myRegex, _ := regexp.Compile(reg)
	found := myRegex.FindString(str)
	if found == "" {
		return "-1"
	}
	return found
}

// 正则提取,返回返回第一个匹配到的内容下标
func (a *Regexp) FindIndex(reg string, str string) string {
	myRegex, _ := regexp.Compile(reg)
	found := myRegex.FindStringIndex(str)
	if found == nil {
		return "-1"
	}
	return strconv.Itoa(found[0]) + "," + strconv.Itoa(found[1])
}

// 正则提取,返回匹配到的所有内容列表,如果没有匹配到,返回-1
func (a *Regexp) FindAll(reg string, str string) string {
	submatch := regexp.MustCompile(reg).FindAllString(str, -1)
	if submatch == nil || len(submatch) == 0 {
		return "-1"
	}
	var addrList = globalfunc.ExecuteFunc("SliceToList", submatch, a.ThreadStore).(string)
	return addrList
}

// 正则提取,返回匹配到的所有内容列表下标,如果没有匹配到,返回-1
func (a *Regexp) FindAllIndex(reg string, str string) string {
	submatch := regexp.MustCompile(reg).FindAllStringIndex(str, -1)
	if submatch == nil || len(submatch) == 0 {
		return "-1"
	}
	//获取下标
	var submatchIndex []string
	for _, v := range submatch {
		submatchIndex = append(submatchIndex, strconv.Itoa(v[0])+","+strconv.Itoa(v[1]))
	}
	var addrList = globalfunc.ExecuteFunc("SliceToList", submatchIndex, a.ThreadStore).(string)
	return addrList
}

// 正则提取子匹配.只返回第一个匹配到的子匹配
func (a *Regexp) FindGroup(reg string, str string) string {
	myRegex, _ := regexp.Compile(reg)
	found := myRegex.FindStringSubmatch(str)
	if found == nil {
		return "-1"
	}
	return found[1]
}

// 正则提取子匹配.只返回第一个匹配到的子匹配下标
func (a *Regexp) FindGroupIndex(reg string, str string) string {
	myRegex, _ := regexp.Compile(reg)
	found := myRegex.FindStringSubmatchIndex(str)
	if found == nil {
		return "-1"
	}
	return strconv.Itoa(found[0]) + "," + strconv.Itoa(found[1])
}

// 正则提取子匹配.返回所有匹配到的子匹配
func (a *Regexp) FindAllGroup(reg string, str string) string {
	myRegex, _ := regexp.Compile(reg)
	found := myRegex.FindAllStringSubmatch(str, -1)
	if found == nil {
		return "-1"
	}
	//取出子匹配
	var submatch []string
	for _, v := range found {
		for _, v1 := range v[1:] {
			submatch = append(submatch, v1)
		}
	}
	var addrList = globalfunc.ExecuteFunc("SliceToList", submatch, a.ThreadStore).(string)
	return addrList
}

// 正则提取子匹配.返回所有匹配到的子匹配下标
func (a *Regexp) FindAllGroupIndex(reg string, str string) string {
	myRegex, _ := regexp.Compile(reg)
	found := myRegex.FindAllStringSubmatchIndex(str, -1)
	if found == nil {
		return "-1"
	}
	//取出子匹配
	var submatchIndex []string
	for _, v := range found {
		for i, v1 := range v[2:] {
			//偶数添加
			if v1%2 == 0 {
				submatchIndex = append(submatchIndex, strconv.Itoa(v[i])+","+strconv.Itoa(v[i+1]))
			}
		}

	}
	var addrList = globalfunc.ExecuteFunc("SliceToList", submatchIndex, a.ThreadStore).(string)
	return addrList
}

func (f *Regexp) GetDef() string {
	return `
		//参数说明: $1:正则表达式,$2:字符串,$3:替换字符串
		@CodeManual(def="Regexp.xxx",des="正则表达式操作")
		type share Regexp{
			def init{
				core&CreateTypeDefault()
			}
		
			@CodeManual(def="Regexp.verify(?)",des="验证正则表达式是否正确")
			def verify{
				to core&Verify($1)
			}
			@CodeManual(def="Regexp.match($1,$2)",des="匹配正则表达式,返回true,false")
			def match{
				to core&Match($1,$2)
			}
			@CodeManual(def="Regexp.replace(?)",des="替换正则表达式,返回替换后的字符串")
			def replace{
				to core&Replace($1,$2,$3)
			}
			@CodeManual(def="Regexp.find($1,$2)",des="提取正则表达式,返回第一个匹配到的")
			def find{
				to core&Find($1,$2)
			}
			@CodeManual(def="Regexp.findIndex($1,$2)",des="提取正则表达式,返回第一个匹配到的下标")
			def findIndex{
				to core&FindIndex($1,$2)
			}
			@CodeManual(def="Regexp.findAll($1,$2)",des="提取正则表达式,返回所有匹配到的列表")
			def findAll{
				to core&FindAll($1,$2)
			}
			@CodeManual(def="Regexp.findAllIndex($1,$2)",des="提取正则表达式,返回所有匹配到的列表下标")
			def findAllIndex{
				to core&FindAllIndex($1,$2)
			}
			@CodeManual(def="Regexp.findGroup($1,$2)",des="提取正则表达式子匹配,返回第一个匹配到的")
			def findGroup{
				to core&FindGroup($1,$2)
			}
			@CodeManual(def="Regexp.findGroupIndex($1,$2)",des="提取正则表达式子匹配,返回第一个匹配到的下标")
			def findGroupIndex{
				to core&FindGroupIndex($1,$2)
			}

			@CodeManual(def="Regexp.findAllGroup($1,$2)",des="提取正则表达式子匹配,返回所有匹配到的列表")
			def findAllGroup{
				to core&FindAllGroup($1,$2)
			}
			@CodeManual(def="Regexp.findAllGroupIndex($1,$2)",des="提取正则表达式子匹配,返回所有匹配到的列表下标")
			def findAllGroupIndex{
				to core&FindAllGroupIndex($1,$2)
			}
			
		
		}
	`
}

func init() {
	RegisterTypeDefault(&Regexp{})
}
func (f *Regexp) GetTypeName() string {
	return "Regexp"
}
